package demo3;

import java.util.Objects;

class Person {
    public String name;
    public int age;
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

//    @Override
//    public boolean equals(Object obj) {
//        Person person = (Person) obj;   //向下转型，obj里没有name，因此需要将obj转为person
//        if(this.name.equals(person.name)) {
//            return true;
//        } else {
//            return false;
//        }
//    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return age == person.age && Objects.equals(name, person.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, age);
    }
}

public class Test1 {

    public static void main(String[] args) {
        String str1="Hello";
        String str2="hello";

        System.out.println(str1.equals(str2));
        System.out.println(str1.equalsIgnoreCase(str2));//忽略大小写地比较字符串
    }


    public static void main5(String[] args) {
        String str1="hello";
        String str2="hello";
        System.out.println(str1 == str2);

        String str3=new String("abcdef");
        String str4=new String("abcdef");
        System.out.println(str4 == str3);
        System.out.println(str1.equals(str2));
    }
    public static void main4(String[] args) {
        String str="abcde";
        String s1=new String("hello");
        String s2=new String("world");
        System.out.println(str.length());

        String str2=null;
        System.out.println(str2);
//        System.out.println(str2.length());

        String str3="";
        System.out.println(str3.length());
        System.out.println(str3.isEmpty());
        System.out.println(str.isEmpty());
    }

    public static void main3(String[] args) {
        //以直接赋值方式定义字符串
        String str="abcd";

        String str2=new String("hello");//和上面的那种相比，没有区别，上面的可以看做是下面的简写

        char[] array={'a','b','c'};//用字符数组构造字符串
        String str3=new String(array);

        System.out.println(str);
        System.out.println(str2);
        System.out.println(str3);
    }

    public static void main2(String[] args) {
        Person person1=new Person("zhangsan",1);
        Person person2=new Person("zhangsan",1);
        System.out.println(person1.hashCode());
        System.out.println(person2.hashCode());
    }

    public static void main1(String[] args) {
        Person person1=new Person("zhangsan",1);
        Person person2=new Person("zhangsan",1);
        System.out.println(person1.equals(person2));
        System.out.println(person1 == person2);
    }
}
